package com.chuanke.ckfamily.action.common;

import com.chuanke.ckfamily.action.base.Response;
import com.chuanke.ckfamily.dao.common.model.Equipment;
import com.chuanke.ckfamily.service.base.FailCode;
import com.chuanke.ckfamily.service.common.EquipmentService;
import com.chuanke.ckfamily.service.common.SysConfigService;
import com.weijie.core.util.Constants;
import com.weijie.core.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangbin on 2017/7/21.
 */
@RequestMapping("/api/interface")
@Controller
public class InterfaceController {

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private SysConfigService sysConfigService;

    private Logger log = LoggerFactory.getLogger(InterfaceController.class);

    @RequestMapping("/info")
    @ResponseBody
    public Response getInterface(HttpServletRequest request) {
        String imie = request.getHeader(ApiCheckerFilter.HEAD_IMEI);
        log.debug("get interface imie={}", imie);
        if (StringUtils.isEmpty(imie)) {
            return new Response(FailCode.APP, "");
        }

        Equipment equipment = equipmentService.getEqupment(imie);
        if (equipment == null
                || (equipment.getIntest() != null && equipment.getIntest() != 1)) {
            return new Response(FailCode.APP, "");
        }

        String api = sysConfigService.getValue("testApi");
        if (StringUtils.isEmpty(api)) {
            api = "http://118.190.44.203";
        }

        Map<String, String> result = new HashMap<>();
        result.put("api", api);
        result.put("model", equipment.getModel());
        return new Response(result);
    }
}